package client.GUI;

import game.Card;
import game.Player;
import game.client.Table;
import game.server.ServerTable;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.print.DocFlavor.CHAR_ARRAY;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import java.awt.Insets;
import javax.swing.JTextField;
import java.awt.Component;
import javax.swing.Box;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JTextPane;
import javax.swing.JLabel;

import server.Message;
import server.utils.CommandType;

import client.Client;
import javax.swing.JTextArea;
import java.awt.GridLayout;

public class Tabel extends JPanel{
	private JTextField textField_Raise;
	private Table table;
	private Player player;
	private boolean turn;
	private Object turnLock = new Object();
	private JTextField textField_Chat;
	private Client client;
	private JTextArea textArea;
	private JLabel Free_Seat_3;
	private JLabel Free_Seat_2;
	private JLabel Free_Seat_4;
	private JLabel lblPlace1;
	private JLabel Seat1_kaart_1;
	private JLabel Seat1_kaart_2;
	private JLabel Seat2_kaart_1;
	private JLabel Seat2_kaart_2;
	private JLabel Seat3_kaart_1;
	private JLabel Seat3_kaart_2;
	private JLabel Seat4_kaart_1;
	private JLabel Seat4_kaart_2;
	private ArrayList<JLabel> seats;
	private ArrayList<JButton> buttons;
	private ArrayList<JLabel> cards;
	private ArrayList<JLabel> oppMoney;
	private ArrayList<JLabel> playerCards;	
	private JLabel tbl_Card_1;
	private JLabel tbl_Card_2;
	private JLabel tbl_Card_3;
	private JLabel tbl_Card_4;
	private JLabel tbl_Card_5;
	private JLabel lbl_TableBet;
	private JLabel lbl_mybettext;
	private JLabel lbl_MyBet;
	private JLabel lbl_MoneyLeftText;
	private JLabel lbl_MoneyLeft;
	private JLabel lbl_Seat4MoneyText;
	private JLabel lbl_Seat4Money;
	private JLabel lbl_Seat3MoneyText;
	private JLabel lbl_Seat3Money;
	private JLabel lbl_Seat2MoneyText;
	private JLabel lbl_Seat2Money;
	private Window window;
	
	public Tabel(final Window window){
		this.window = window;
		turn = true;
		seats = new ArrayList<JLabel>();
		buttons = new ArrayList<JButton>();
		cards = new ArrayList<JLabel>();
		oppMoney = new ArrayList<JLabel>();
		playerCards = new ArrayList<JLabel>();
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{61, 84, 57, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 34, 53, 0, 42, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 28, 41, 0, 29, 27, 20, 78, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		lbl_Seat3MoneyText = new JLabel("Money :");
		GridBagConstraints gbc_lbl_Seat3MoneyText = new GridBagConstraints();
		gbc_lbl_Seat3MoneyText.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_Seat3MoneyText.gridx = 12;
		gbc_lbl_Seat3MoneyText.gridy = 3;
		add(lbl_Seat3MoneyText, gbc_lbl_Seat3MoneyText);
		
		lbl_Seat3Money = new JLabel("");
		GridBagConstraints gbc_lbl_Seat3Money = new GridBagConstraints();
		gbc_lbl_Seat3Money.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_Seat3Money.gridx = 13;
		gbc_lbl_Seat3Money.gridy = 3;
		add(lbl_Seat3Money, gbc_lbl_Seat3Money);
		
		Seat4_kaart_2 = new JLabel();
		GridBagConstraints gbc_Seat4_kaart_2 = new GridBagConstraints();
		gbc_Seat4_kaart_2.insets = new Insets(0, 0, 5, 5);
		gbc_Seat4_kaart_2.gridx = 19;
		gbc_Seat4_kaart_2.gridy = 5;
		add(Seat4_kaart_2, gbc_Seat4_kaart_2);
		
		Seat2_kaart_1 = new JLabel();
		GridBagConstraints gbc_Seat2_kaart_1 = new GridBagConstraints();
		gbc_Seat2_kaart_1.anchor = GridBagConstraints.SOUTHWEST;
		gbc_Seat2_kaart_1.insets = new Insets(0, 0, 5, 5);
		gbc_Seat2_kaart_1.gridx = 2;
		gbc_Seat2_kaart_1.gridy = 6;
		add(Seat2_kaart_1, gbc_Seat2_kaart_1);

		Seat4_kaart_1 = new JLabel();
		GridBagConstraints gbc_Seat4_kaart_1 = new GridBagConstraints();
		gbc_Seat4_kaart_1.insets = new Insets(0, 0, 5, 5);
		gbc_Seat4_kaart_1.gridx = 19;
		gbc_Seat4_kaart_1.gridy = 8;
		add(Seat4_kaart_1, gbc_Seat4_kaart_1);
		
		Seat3_kaart_2 = new JLabel();
		GridBagConstraints gbc_Seat3_kaart_2 = new GridBagConstraints();
		gbc_Seat3_kaart_2.insets = new Insets(0, 0, 5, 5);
		gbc_Seat3_kaart_2.gridx = 11;
		gbc_Seat3_kaart_2.gridy = 4;
		add(Seat3_kaart_2, gbc_Seat3_kaart_2);
		
		Seat3_kaart_1 = new JLabel();
		GridBagConstraints gbc_Seat3_kaart_1 = new GridBagConstraints();
		gbc_Seat3_kaart_1.insets = new Insets(0, 0, 5, 5);
		gbc_Seat3_kaart_1.gridx = 12;
		gbc_Seat3_kaart_1.gridy = 4;
		add(Seat3_kaart_1, gbc_Seat3_kaart_1);
		
		Seat2_kaart_2 = new JLabel();
		GridBagConstraints gbc_Seat2_kaart_2 = new GridBagConstraints();
		gbc_Seat2_kaart_2.insets = new Insets(0, 0, 5, 5);
		gbc_Seat2_kaart_2.gridx = 2;
		gbc_Seat2_kaart_2.gridy = 7;
		add(Seat2_kaart_2, gbc_Seat2_kaart_2);
		
		lbl_Seat2MoneyText = new JLabel("Money :");
		GridBagConstraints gbc_lbl_Seat2MoneyText = new GridBagConstraints();
		gbc_lbl_Seat2MoneyText.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_Seat2MoneyText.gridx = 1;
		gbc_lbl_Seat2MoneyText.gridy = 9;
		add(lbl_Seat2MoneyText, gbc_lbl_Seat2MoneyText);
		
		lbl_Seat2Money = new JLabel("");
		GridBagConstraints gbc_lbl_Seat2Money = new GridBagConstraints();
		gbc_lbl_Seat2Money.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_Seat2Money.gridx = 2;
		gbc_lbl_Seat2Money.gridy = 9;
		add(lbl_Seat2Money, gbc_lbl_Seat2Money);
		oppMoney.add(lbl_Seat2Money);
		
		Seat1_kaart_2 = new JLabel();
		GridBagConstraints gbc_Seat1_kaart_2 = new GridBagConstraints();
		gbc_Seat1_kaart_2.insets = new Insets(0, 0, 5, 5);
		gbc_Seat1_kaart_2.gridx = 12;
		gbc_Seat1_kaart_2.gridy = 10;
		add(Seat1_kaart_2, gbc_Seat1_kaart_2);
		
		Seat1_kaart_1 = new JLabel();
		GridBagConstraints gbc_Seat1_kaart_1 = new GridBagConstraints();
		gbc_Seat1_kaart_1.insets = new Insets(0, 0, 5, 5);
		gbc_Seat1_kaart_1.gridx = 14;
		gbc_Seat1_kaart_1.gridy = 10;
		add(Seat1_kaart_1, gbc_Seat1_kaart_1);
		
		textArea = new JTextArea();
		textArea.setEditable(false);
		JScrollPane jsp = new JScrollPane(textArea);
		GridBagConstraints gbc_textArea = new GridBagConstraints();
		gbc_textArea.insets = new Insets(0, 0, 5, 5);
		gbc_textArea.gridheight = 3;
		gbc_textArea.fill = GridBagConstraints.BOTH;
		gbc_textArea.gridx = 0;
		gbc_textArea.gridy = 8;
		add(textArea, gbc_textArea);
		//add(jsp, gbc_textArea);
		
		Free_Seat_4 = new JLabel("Free seat");
		GridBagConstraints gbc_lblFreeSeat_1 = new GridBagConstraints();
		gbc_lblFreeSeat_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblFreeSeat_1.gridx = 21;
		gbc_lblFreeSeat_1.gridy = 8;
		add(Free_Seat_4, gbc_lblFreeSeat_1);
		
		Free_Seat_3 = new JLabel("Free seat");
		GridBagConstraints gbc_lbl_MyBet = new GridBagConstraints();
		gbc_lbl_MyBet.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_MyBet.gridx = 12;
		gbc_lbl_MyBet.gridy = 2;
		add(Free_Seat_3, gbc_lbl_MyBet);
		
		Free_Seat_2 = new JLabel("Free seat");
		GridBagConstraints gbc_lblFreeSeat = new GridBagConstraints();
		gbc_lblFreeSeat.insets = new Insets(0, 0, 5, 5);
		gbc_lblFreeSeat.gridx = 1;
		gbc_lblFreeSeat.gridy = 8;
		add(Free_Seat_2, gbc_lblFreeSeat);
		
		lbl_Seat4Money = new JLabel("");
		GridBagConstraints gbc_lbl_Seat4Money = new GridBagConstraints();
		gbc_lbl_Seat4Money.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_Seat4Money.gridx = 20;
		gbc_lbl_Seat4Money.gridy = 10;
		add(lbl_Seat4Money, gbc_lbl_Seat4Money);
		
		lbl_Seat4MoneyText = new JLabel(":Money");
		GridBagConstraints gbc_lbl_Seat4MoneyText = new GridBagConstraints();
		gbc_lbl_Seat4MoneyText.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_Seat4MoneyText.gridx = 21;
		gbc_lbl_Seat4MoneyText.gridy = 10;
		add(lbl_Seat4MoneyText, gbc_lbl_Seat4MoneyText);

		lblPlace1 = new JLabel("User seat");
		GridBagConstraints gbc_lblPlace1 = new GridBagConstraints();
		gbc_lblPlace1.insets = new Insets(0, 0, 5, 5);
		gbc_lblPlace1.gridx = 12;
		gbc_lblPlace1.gridy = 11;
		add(lblPlace1, gbc_lblPlace1);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(0, 100, 0));
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridheight = 4;
		gbc_panel.gridwidth = 16;
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 3;
		gbc_panel.gridy = 5;
		add(panel, gbc_panel);
		panel.setLayout(new GridLayout(1, 0, 0, 0));
		
		tbl_Card_5 = new JLabel();
		panel.add(tbl_Card_5);
		
		tbl_Card_4 = new JLabel();
		panel.add(tbl_Card_4);
		
		lbl_TableBet = new JLabel("");
		panel.add(lbl_TableBet);
		
		tbl_Card_3 = new JLabel();
		panel.add(tbl_Card_3);
		
		tbl_Card_2 = new JLabel();
		panel.add(tbl_Card_2);
		
		tbl_Card_1 = new JLabel();
		panel.add(tbl_Card_1);
		
		textField_Chat = new JTextField();
		GridBagConstraints gbc_textField_Chat = new GridBagConstraints();
		gbc_textField_Chat.insets = new Insets(0, 0, 5, 5);
		gbc_textField_Chat.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_Chat.gridx = 0;
		gbc_textField_Chat.gridy = 11;
		add(textField_Chat, gbc_textField_Chat);
		textField_Chat.setColumns(10);
		
		JButton btnSend = new JButton("Send message");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textArea.append(client.getName()+"> "+textField_Chat.getText()+"\n");
				client.sendMessage(new Message(player.getName(), CommandType.CHAT, textField_Chat.getText()));
				textField_Chat.setText("");
			}
		});
		GridBagConstraints gbc_btnSend = new GridBagConstraints();
		gbc_btnSend.insets = new Insets(0, 0, 5, 5);
		gbc_btnSend.gridx = 0;
		gbc_btnSend.gridy = 12;
		add(btnSend, gbc_btnSend);
		
		lbl_MoneyLeftText = new JLabel("Your Money:");
		GridBagConstraints gbc_lbl_MoneyLeftText = new GridBagConstraints();
		gbc_lbl_MoneyLeftText.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_MoneyLeftText.gridx = 8;
		gbc_lbl_MoneyLeftText.gridy = 12;
		add(lbl_MoneyLeftText, gbc_lbl_MoneyLeftText);
		
		lbl_MoneyLeft = new JLabel("");
		GridBagConstraints gbc_lbl_MoneyLeft = new GridBagConstraints();
		gbc_lbl_MoneyLeft.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_MoneyLeft.gridx = 9;
		gbc_lbl_MoneyLeft.gridy = 12;
		add(lbl_MoneyLeft, gbc_lbl_MoneyLeft);
		
		textField_Raise = new JTextField();
		GridBagConstraints gbc_textField_Raise = new GridBagConstraints();
		gbc_textField_Raise.insets = new Insets(0, 0, 5, 5);
		gbc_textField_Raise.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_Raise.gridx = 16;
		gbc_textField_Raise.gridy = 12;
		add(textField_Raise, gbc_textField_Raise);
		textField_Raise.setColumns(10);
		
		JButton btnQuit = new JButton("Quit");
		btnQuit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				window.quit();
			}
		});
		GridBagConstraints gbc_btnQuit = new GridBagConstraints();
		gbc_btnQuit.insets = new Insets(0, 0, 0, 5);
		gbc_btnQuit.gridx = 0;
		gbc_btnQuit.gridy = 13;
		add(btnQuit, gbc_btnQuit);
		
		JButton btnRaise = new JButton("Raise");
		btnRaise.setEnabled(!turn);
		btnRaise.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double bet;
				try {
					double maxBet = getMaxBet() - player.getCurrentBet();
					bet = Double.parseDouble(textField_Raise.getText());
					if (bet < maxBet) {
						ErrorWindow ew = new ErrorWindow("You must bet more!", window);
					} else {
						makeBet(bet);
					}
				} catch(NumberFormatException nfe) {
					ErrorWindow ew = new ErrorWindow("Enter numeric value!", window);
				}
				textField_Raise.setText("");
			}
		});
		
		lbl_mybettext = new JLabel("Betting:");
		GridBagConstraints gbc_lbl_mybettext = new GridBagConstraints();
		gbc_lbl_mybettext.insets = new Insets(0, 0, 0, 5);
		gbc_lbl_mybettext.gridx = 8;
		gbc_lbl_mybettext.gridy = 13;
		add(lbl_mybettext, gbc_lbl_mybettext);
		
		lbl_MyBet = new JLabel("");
		GridBagConstraints gbc_lbl_MyBet1 = new GridBagConstraints();
		gbc_lbl_MyBet1.insets = new Insets(0, 0, 0, 5);
		gbc_lbl_MyBet1.gridx = 10;
		gbc_lbl_MyBet1.gridy = 13;
		add(lbl_MyBet, gbc_lbl_MyBet1);
		GridBagConstraints gbc_btnRaise = new GridBagConstraints();
		gbc_btnRaise.gridwidth = 4;
		gbc_btnRaise.insets = new Insets(0, 0, 0, 5);
		gbc_btnRaise.gridx = 13;
		gbc_btnRaise.gridy = 13;
		add(btnRaise, gbc_btnRaise);
		
		JButton btnNewButton_1 = new JButton("Call/Check");
		btnNewButton_1.setEnabled(!turn);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double bet = getMaxBet() - player.getCurrentBet();
				if (bet < 0) {
					ErrorWindow ew = new ErrorWindow("Negative bet!", window);
				} else {
					makeBet(bet);
				}
			}
		});
		GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();
		gbc_btnNewButton_1.gridwidth = 2;
		gbc_btnNewButton_1.insets = new Insets(0, 0, 0, 5);
		gbc_btnNewButton_1.gridx = 17;
		gbc_btnNewButton_1.gridy = 13;
		add(btnNewButton_1, gbc_btnNewButton_1);
		
		JButton btnNewButton = new JButton("Fold");
		btnNewButton.setEnabled(!turn);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Player player = table.getPlayer();
				player.clearCards();
				client.sendMessage(new Message(player.getName(),
						CommandType.BET, "-1"));
				setTurn(true);
			}
		});
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);
		gbc_btnNewButton.gridx = 21;
		gbc_btnNewButton.gridy = 13;
		add(btnNewButton, gbc_btnNewButton);
		
		seats.add(Free_Seat_2);
		seats.add(Free_Seat_3);
		seats.add(Free_Seat_4);
		buttons.add(btnNewButton);
		buttons.add(btnNewButton_1);
		buttons.add(btnRaise);
		cards.add(tbl_Card_1);
		cards.add(tbl_Card_2);
		cards.add(tbl_Card_3);
		cards.add(tbl_Card_4);
		cards.add(tbl_Card_5);
		oppMoney.add(lbl_Seat3Money);
		oppMoney.add(lbl_Seat4Money);
		playerCards.add(Seat1_kaart_1);
		playerCards.add(Seat1_kaart_2);
		playerCards.add(Seat2_kaart_1);
		playerCards.add(Seat2_kaart_2);
		playerCards.add(Seat3_kaart_1);
		playerCards.add(Seat3_kaart_2);
		playerCards.add(Seat4_kaart_1);
		playerCards.add(Seat4_kaart_2);
	}
	/**
	 * Syncs clients table and player/opponents info with server.
	 * @param msg Message from server with neccessary info
	 * 		  @ Servertable field.
	 */
	public void dataSync(Message msg) {
		Table tbl = msg.getTable();
		//update player's info
		player.clearCards(); //me
		lbl_MyBet.setText(Double.toString(player.getCurrentBet()));
		lbl_MoneyLeft.setText(Double.toString(player.getMoneyLeft()));
		for (Player p : table.getOpponents()) { //opponents
			p.clearCards();
			paintOpponent(p);
		}
//		boolean b = false;
//		for (Player p : tbl.getOpponents()) {
//			String name = p.getName();
//			if (!b && table.getPlayer().getName().equalsIgnoreCase(name)) { //sync my info
//				player = p;
//				// display @ GUI (player's money, current bet, cards(empty))
//				b = true;
//			} else {
//				for (Player o : table.getOpponents()) {
//					if (o.getName().equalsIgnoreCase(name)) { //sync opponent info
//						o = p;
//						table.removeOpponent(o);
//						table.addOpponent(p);
//						// display @ GUI (player's money, current bet, cards(empty))
//						break;
//					}
//				}
//			}
//		}
		table.setBetOnTable(tbl.getBetOnTable());
		for(JLabel l : cards){
			l.setIcon(null);
			l.setName(null);
		}
		for (JLabel l : playerCards) {
			l.setIcon(null);
		}
		table.clearCardsOnTable();
		lbl_TableBet.setText(Double.toString(table.getBetOnTable()));
	}
	/**
	 * Find currently max bet.
	 * @return Max bet.
	 */
	private double getMaxBet() {
		double curMaxBet = player.getCurrentBet();
		for (Player p : table.getOpponents()) {
			double b = p.getCurrentBet();
			if (b > curMaxBet) {
				curMaxBet = b;
			}
		}
		return curMaxBet;
	}
	/**
	 * Makes bet for this player. Sends out message object with 
	 * the value of that bet, type CommandType.BET.
	 * Also adds bet to the table and cahnges player's turn to wait.
	 * @param bet
	 */
	private void makeBet(double bet){
		Player player = table.getPlayer();
		player.bet(bet);
		table.addBetToTable(bet);
		client.sendMessage(new Message(player.getName(),
				CommandType.BET, String.valueOf(bet)));
		setTurn(true);
		lbl_TableBet.setText(Double.toString(table.getBetOnTable()));
		lbl_MyBet.setText(Double.toString(player.getCurrentBet()));
		lbl_MoneyLeft.setText(Double.toString(player.getMoneyLeft()));
		//TODO disp @ GUI
	}
	/**
	 * Adds opponents bet to GUI and saves data.
	 * @param bet Opponent's bet
	 */
	public void opponentBet(String name, double bet) {
		for (Player p : table.getOpponents()) {
			if (p.getName().equalsIgnoreCase(name)) {
				if (bet < 0) { //folds
					p.clearCards();
					//TODO disp @ GUI
					break;
				}
				p.bet(bet);
				paintOpponent(p);
				table.addBetToTable(bet);
				lbl_TableBet.setText(Double.toString(table.getBetOnTable()));
				//TODO disp @ GUI
				break;
			}
		}
	}
	/**
	 * Repaints player's info.
	 * @param p
	 */
	private void paintOpponent(Player p) {
		if(Free_Seat_2.getText().equalsIgnoreCase(p.getName())){
			lbl_Seat2Money.setText(Double.toString(p.getMoneyLeft()));
		}
		if(Free_Seat_3.getText().equalsIgnoreCase(p.getName())){
			lbl_Seat3Money.setText(Double.toString(p.getMoneyLeft()));
		}
		if(Free_Seat_4.getText().equalsIgnoreCase(p.getName())){
			lbl_Seat4Money.setText(Double.toString(p.getMoneyLeft()));
		}
	}
	/**
	 * Sets client.
	 * @param client Client to set
	 */
	public void setClient(Client client) {
		this.client = client;
	}
	/**
	 * Adds chat message to chatmessage area.
	 * @param msg Message to display
	 */
	public void chat(Message msg) {
		textArea.append(msg.getFrom() + "> "+msg.getCommandValue()+"\n");
	}
	/**
	 * Deals money to opponent.
	 * @param money
	 */
	public void dealOpponentMoney(String name, double money) {
		for (Player p :table.getOpponents()) {
			if (p.getName().equalsIgnoreCase(name)) {
				p.addMoney(money);
				break;
			}
		}
	}
	/**
	 *  Deals money to himself.
	 * @param money Money to deal
	 */
	public void dealMoney(double money) {
		this.player.addMoney(money);
	}
	/**
	 * Deals card on the table.
	 * @param card Card to deal
	 */
	public void dealCardToTable(Card card) {
		ImageIcon image = new ImageIcon("src\\Images\\"+card.getSuit().trim()+""+card.getValue()+".jpg");
		this.table.addCard(card);
		for(JLabel l : cards){
			if(l.getName()==null){
				l.setName(card.getSuit()+""+card.getValue());
				l.setIcon(image);
				break;
			}
		}
	}
	/**
	 * Deals card to desired player.
	 * @param name Player who has to recive card
	 * @param card Card to deal
	 */
	public void addCardToPlayer(String name, Card card) {
		ImageIcon image = new ImageIcon("src\\Images\\"+card.getSuit().trim()+""+card.getValue()+".jpg");
		if(client.getName().equalsIgnoreCase(name)){
			this.table.getPlayer().dealCard(card);
			if(Seat1_kaart_1.getIcon()== null){
				Seat1_kaart_1.setIcon(image);
			}else{Seat1_kaart_2.setIcon(image);}
		}else if(this.table.getOpponents() != null) {
			for(Player p : this.table.getOpponents()){
				if(p.getName().equalsIgnoreCase(name)){
					p.dealCard(card);
				}
				if(!Free_Seat_2.getText().equals("Free seat")){
					Seat2_kaart_1.setIcon(image);
					Seat2_kaart_2.setIcon(image);
				}
				if(!Free_Seat_3.getText().equals("Free seat")){
					Seat3_kaart_1.setIcon(image);
					Seat3_kaart_2.setIcon(image);
				}
				if(!Free_Seat_4.getText().equals("Free seat")){
					Seat4_kaart_1.setIcon(image);
					Seat4_kaart_2.setIcon(image);
				}
			}
		}
	}
	/**
	 * Sets opponents behind the table.
	 * @param opponents Opponents to set.
	 */
	public void setOpponents(ArrayList<Player> opponents) {
		if (opponents != null) {
			this.table.setOpponents(opponents);
		}
		for(Player p : opponents){
			for(JLabel l : seats){
				if(l.getText().equalsIgnoreCase("free seat")){
					l.setText(p.getName());
					l.setName(p.getName());
					paintOpponent(p);
					break;
				}
			}
		}
	}
	/**
	 * Edits opponents at the table.
	 * If opponent already exists behind the table removes it, 
	 * if not adds it to the table.
	 * @param opponent
	 */
	public void editOpponent(String opponent) {
		ArrayList<Player> opponents = table.getOpponents();
		Player tmp = null;
		for (Player p : opponents) {
			if (p.getName().equalsIgnoreCase(opponent)) {
				tmp = p;
				break;
			}
		}
		if (tmp != null) {
			table.removeOpponent(tmp);
			for(JLabel l : seats){
				if(l.getText().equalsIgnoreCase(tmp.getName())){
					l.setText("Free seat");
					l.setName("Free seat");
					break;
				}
			}
			tmp.setMoneyZero();
			paintOpponent(tmp);
			
		} else {
			Player p = new Player(opponent, Client.conf.getInitialMoney());
			table.addOpponent(p);
			for(JLabel l : seats){
				if(l.getText().equalsIgnoreCase("free seat")){
					l.setText(opponent);
					l.setName(opponent);
					paintOpponent(p);
					break;
				}
			}
		}
	}
	/**
	 * @param table the table to set
	 */
	public synchronized void setTable(Table table) {
		this.table = table;
	}
	/**
	 * @param player the player to set
	 */
	public synchronized void setPlayer(Player player) {
		this.player = player;
		lblPlace1.setText(player.getName());
		lblPlace1.setName(player.getName());
		lbl_MoneyLeft.setText(Double.toString(player.getMoneyLeft()));
	}
	/**
	 * @return the turn
	 */
	public boolean isTurn() {
		synchronized (turnLock) {
			return turn;
		}
	}
	/**
	 * @param turn the turn to set
	 */
	public void setTurn(boolean turn) {
		synchronized (turnLock) {
			for (JButton button : buttons) {
				button.setEnabled(!turn);
			}
			textField_Raise.setEnabled(!turn);
			this.turn = turn;
		}
	}
	/**
	 * @return the window
	 */
	public synchronized Window getWindow() {
		return window;
	}
	
}
